package cn.ruanyun.backInterface.modules.business.profitDetail.serviceimpl;

import cn.ruanyun.backInterface.common.enums.ProfitDetailEnum;
import cn.ruanyun.backInterface.modules.base.service.mybatis.IUserService;
import cn.ruanyun.backInterface.modules.business.order.service.IOrderService;
import cn.ruanyun.backInterface.modules.business.profitDetail.mapper.ProfitDetailMapper;
import cn.ruanyun.backInterface.modules.business.profitDetail.pojo.ProfitDetail;
import cn.ruanyun.backInterface.modules.business.profitDetail.service.IProfitDetailService;
import cn.ruanyun.backInterface.modules.business.profitDetail.vo.ProfitDetailCount;
import cn.ruanyun.backInterface.modules.business.profitDetail.vo.ProfitDetailVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import cn.ruanyun.backInterface.common.utils.ToolUtil;


/**
 * 分红明细接口实现
 * @author z
 */
@Slf4j
@Service
@Transactional
public class IProfitDetailServiceImpl extends ServiceImpl<ProfitDetailMapper, ProfitDetail> implements IProfitDetailService {


    @Autowired
    private IUserService userService;

    @Autowired
    private IOrderService orderService;



    @Override
    public List<ProfitDetailVo> getProfitDetailList() {

        return Optional.ofNullable(ToolUtil.setListToNul(this.list(Wrappers.<ProfitDetail>lambdaQuery()
        .orderByDesc(ProfitDetail::getCreateTime))))
        .map(profitDetails -> profitDetails.parallelStream().flatMap(profitDetail -> {


            ProfitDetailVo profitDetailVo = new ProfitDetailVo();

            //平台信息
            if (ToolUtil.isEmpty(profitDetail.getCreateBy())) {

                profitDetailVo.setNickName("平台");
            }

            //用户信息
            Optional.ofNullable(userService.getById(profitDetail.getCreateBy()))
                    .ifPresent(user -> {

                        ToolUtil.copyProperties(user, profitDetailVo);
                    });

            //订单信息
            Optional.ofNullable(orderService.getById(profitDetail.getOrderId()))
                    .ifPresent(order -> {

                        ToolUtil.copyProperties(order, profitDetailVo);

                        //分账金额信息
                        profitDetailVo.setProfitTotalMoney(order.getTotalPrice())
                                .setProfitMoney(profitDetail.getProfitMoney());

                    });

            ToolUtil.copyProperties(profitDetail, profitDetailVo);

            return Stream.of(profitDetailVo);
        }).collect(Collectors.toList())).orElse(null);



    }

    @Override
    public ProfitDetailCount getProfitDetailVo() {


        return Optional.ofNullable(ToolUtil.setListToNul(this.list(Wrappers.<ProfitDetail>lambdaQuery()
                .eq(ProfitDetail::getProfitDetailEnum, ProfitDetailEnum.PLATFORM))))
                .map(profitDetails -> {

                    ProfitDetailCount profitDetailCount = new ProfitDetailCount();

                    // TODO: 2020/5/14 0014  日收入
                    List<ProfitDetail> day = profitDetails.parallelStream().filter(profitDetail ->
                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(profitDetail.getCreateTime()).contains(getToday()))
                            .collect(Collectors.toList());
                    profitDetailCount.setDayIncome(day.parallelStream().map(ProfitDetail::getProfitMoney)
                            .reduce(BigDecimal.ZERO, BigDecimal::add));
                    // TODO: 2020/5/14 0014  月收入
                    List<ProfitDetail> month = profitDetails.parallelStream().filter(profitDetail ->
                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(profitDetail.getCreateTime()).contains(getMonth())).collect(Collectors.toList());
                    profitDetailCount.setMonthIncome(month.parallelStream().map(ProfitDetail::getProfitMoney)
                            .reduce(BigDecimal.ZERO, BigDecimal::add));
                    // TODO: 2020/5/14 0014  年收入
                    List<ProfitDetail> year = profitDetails.parallelStream().filter(profitDetail ->
                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(profitDetail.getCreateTime()).contains(getYear())).collect(Collectors.toList());
                    profitDetailCount.setYearIncome(year.parallelStream().map(ProfitDetail::getProfitMoney)
                            .reduce(BigDecimal.ZERO, BigDecimal::add));
                    //总收入
                    profitDetailCount.setTotalIncome(profitDetails.parallelStream().map(ProfitDetail::getProfitMoney)
                            .reduce(BigDecimal.ZERO, BigDecimal::add));

                    return profitDetailCount;

                }).orElse(new ProfitDetailCount());

    }

    /**
     * 当天
     * @return
     */
    public String getToday(){
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }

    /**
     * 当月
     * @return
     */
    public String getMonth(){
        return new SimpleDateFormat("yyyy-MM").format(new Date());
    }

    /**
     * 当年
     * @return
     */
    public String getYear(){
        return new SimpleDateFormat("yyyy").format(new Date());
    }

}